﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Security.Claims;
using Zhao.Common.EnumEntity;
using Zhao.Common.Helper;
using Zhao.IServer.Events;
using Zhao.Models.DTO;
using Zhao.Models.Entity;

namespace Zhao.Arena.Controllers.ActivityManagement
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ActivityMembersController : ControllerBase
    {
        private readonly IActivityMembersServer _activityMembersServer;
        private readonly IActivitiesServer _activitiesServer;
        public ActivityMembersController(IActivityMembersServer activityMembersServer, IActivitiesServer activitiesServer)
        {
            this._activityMembersServer = activityMembersServer;
            this._activitiesServer = activitiesServer;
        }

        /// <summary>
        /// 根据活动ID查询对应的报名人数
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{pageIndex:int}/{pageSize:int}/{id:int}")]
        public async Task<JsonResult> GetActivityMembers(int id,int pageIndex=1,int pageSize=10)
        {
            var result = new ApiResult<PageModel<ActivityMembersDto>>() { Success = false, Message = "操作失败" };

            string? venueIdClaim = User.FindFirst("VenueId")?.Value;

            Guid.TryParse(venueIdClaim, out Guid venueId);

            Expressionable<ActivityMembers, Users, Activities> expressionable = new Expressionable<ActivityMembers, Users, Activities>();

            expressionable.AndIF(venueId != default, (a, b, c) => c.VenueID.Equals(venueId));

            expressionable.And((a, b, c) => c.ActivityID == id);

            var data = await _activityMembersServer.QueryActivityMembersPage(expressionable.ToExpression(), pageIndex, pageSize);

            if (data != null)
            {
                result.Data = data;
                result.Success = true;
                result.Message = "成功";
            }

            return new JsonResult(result);
        }

    }
}
